package com.atguigu.cloud.controller;

import cn.hutool.core.date.DateUtil;
import com.atguigu.cloud.apis.PayFeignApi;
import com.atguigu.cloud.entities.PayDTO;
import com.atguigu.cloud.resp.ResultCodeEnum;
import com.atguigu.cloud.resp.ResultData;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * ClassName:    OrderController
 * Package:  com.atguigu.cloud.controller
 * Description:
 *
 * @Author: interface
 * @Create: 2025/7/16 - 上午7:02
 * @Version: v1.0
 */
@RestController
public class OrderController {


    private static final Logger log = LoggerFactory.getLogger(OrderController.class);
    @Resource
    private PayFeignApi payFeignApi;

    @PostMapping("/feign/pay/add")
    public ResultData addOrder(@RequestBody PayDTO payDTO) {
        System.out.println("第一步：模拟本地addOrder新增订单成功(省略sql操作)，第二步：再开启addPay支付微服务远程调用");
        ResultData<String> resultData = payFeignApi.addPay(payDTO);
        return resultData;

    }

    @DeleteMapping("/feign/pay/del/{id}")
    public ResultData delOrder(@PathVariable("id") Integer id) {
        payFeignApi.deletePay(id);
        return ResultData.success("删除成功");
    }

    @PutMapping(value = "/feign/pay/update")
    public ResultData updateOrder(@RequestBody PayDTO payDTO) {
        ResultData<String> resultData = payFeignApi.updatePay(payDTO);
        return resultData;
    }

    @GetMapping(value = "/feign/pay/get/{id}")
    public ResultData getPay(@PathVariable("id") int id) {
        System.out.println("-----------支付微服务远程调用,按照id查询订单支付流水信息");
        ResultData resultData = null;
        try {
            System.out.println("----调用开始: "+ DateUtil.now());
            resultData = payFeignApi.getPay(id);
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("----调用结束: "+ DateUtil.now());
            return ResultData.error(ResultCodeEnum.RC500.getCode(),e.getMessage());
        }

        return resultData;
    }

    @GetMapping(value = "/feign/pay/all")
    public ResultData getAllPay() {
        ResultData resultData = payFeignApi.getAllPay();
        return resultData;
    }

    @GetMapping(value="/feign/pay/get/info")
    public ResultData<String> getPayDTO(){
        System.out.println("-------支付微服务远程调用，按照id查询订单支付流水信息");
        ResultData<String> resultData = payFeignApi.getPayDTO();
        return resultData;
    }


    @Resource
    private DiscoveryClient discoveryClient;

    @GetMapping("/feign/discovery")
    public String discovery(){
        List<String> services = discoveryClient.getServices();
        for (String element : services) {
            System.out.println(element);
        }

        System.out.println("===================================");

        List<ServiceInstance> instances = discoveryClient.getInstances("cloud-payment-service");
        for (ServiceInstance element : instances) {
            System.out.println(element.getServiceId()+"\t"+element.getHost()+"\t"+element.getPort()+"\t"+element.getUri());
        }

        return instances.get(0).getServiceId()+":"+instances.get(0).getPort();
    }
}
